package com.dpwx.module.startup.view;

import org.puremvc.java.multicore.interfaces.INotification;

import com.dpwx.framework.mvc.BaseActivity;
import com.dpwx.framework.mvc.BaseMediator;
import com.dpwx.framework.mvc.BaseView;
import com.dpwx.framework.network.FakeNetworkTask;
import com.dpwx.framework.network.INetworkCallback;
import com.dpwx.module.startup.StartupFacade;

public class SplashMediator extends BaseMediator 
implements INetworkCallback{
	
	public SplashMediator(BaseActivity activity, String name) {
		super(activity, name);
	}

	@Override
	public String[] listNotificationInterests() {
		return new String[]{StartupFacade.CMD_SHOW_SPLASH, StartupFacade.CMD_SHOW_LOGIN};
	}
	
	@Override
	public void handleNotification(INotification notification) {
		if (StartupFacade.CMD_SHOW_SPLASH.equals(notification.getName())) {
			getViewComponent().show();
			getFacade().removeMediator(multitonKey);
			getActivity().clearViewStack();
			getFacade().registerMediator(new LoginMediator(getActivity(), StartupFacade.MEDIATOR_LOGIN));
			getActivity().getUiEventDispatcher().doTask(new FakeNetworkTask(null, null, this));
		}
	}

	@Override
	public BaseView onCreateView() {
		return new SplashView(getActivity());
	}

	@Override
	public void networkFinished(byte[] data) {
		sendNotification(StartupFacade.CMD_SHOW_LOGIN);
	}

	@Override
	public void networkError(byte[] data) {
		sendNotification(StartupFacade.CMD_SHOW_LOGIN);
		getActivity().getUiEventDispatcher().showToast("startup failed");
	}

}
